package org.huqiwen.blog.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.registry.SaveException;
import javax.xml.soap.Detail;

import org.huqiwen.blog.data.BlogDataBean;
import org.huqiwen.blog.data.CommentDataBean;
import org.huqiwen.blog.logic.BlogLogicBean;
import org.huqiwen.blog.logic.CommentLogicBean;

public class BlogActionServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = (String) request.getParameter("method");
		if (method == null) {
			PrintWriter out = response.getWriter();
			out.println("无效的请求");
		} else if (method.equals("addBlog")) {
			AddBlog(request, response);
		} else if (method.equals("delBlog")) {
			DelBlog(request, response);
		} else if (method.equals("editorBlog")) {
			EditorBlog(request, response);
		} else if (method.equals("saveBlog")) {
			SaveBlog(request, response);
		} else if (method.equals("detailBlog")) {
			DetailBlog(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	protected void AddBlog(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String blogTitle = request.getParameter("blogTitle");
		String blogContent = request.getParameter("blogContent");
		String catgory = request.getParameter("category");
		int result = new BlogLogicBean().addBlog(blogTitle, blogContent,
				catgory);
		if (result == 1) {
			request.getRequestDispatcher("index.jsp?curPage=1").forward(
					request, response);
		} else {
			request.getRequestDispatcher("error.jsp")
					.forward(request, response);
		}
	}

	protected void DelBlog(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int result = new BlogLogicBean().delBlog(id);
		if (result == 1) {
			response.sendRedirect("index.jsp");
			// request.getRequestDispatcher("index.jsp").forward(request,
			// response);
		} else {
			request.getRequestDispatcher("error.jsp")
					.forward(request, response);
		}
	}

	protected void EditorBlog(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		BlogDataBean bean = new BlogLogicBean().findBlogById(id);
		if (null != bean) {
			request.setAttribute("bean", bean);
			// response.sendRedirect("admin/EditorBlog.jsp?id="+id);
			request.getRequestDispatcher("admin/EditorBlog.jsp").forward(
					request, response);
		} else {
			request.getRequestDispatcher("error.jsp")
					.forward(request, response);
		}
	}

	protected void SaveBlog(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String blogId = request.getParameter("id");
		String blogTitle = request.getParameter("blogTitle");
		String blogContent = request.getParameter("blogContent");
		String catgory = request.getParameter("category");
		int result = new BlogLogicBean().editorBlog(blogId, blogTitle,
				blogContent, catgory);
		if (result == 1) {
			request.getRequestDispatcher("index.jsp?curPage=1").forward(
					request, response);
		} else {
			request.getRequestDispatcher("error.jsp")
					.forward(request, response);
		}
	}

	protected void DetailBlog(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String curPage = request.getParameter("curPage");
		BlogDataBean bean = new BlogLogicBean().findBlogById(id);
		if (null != bean) {
			request.setAttribute("bean", bean);
			if (curPage==null) {
				request.getRequestDispatcher("detail.jsp").forward(request,
						response);
			}else {
				request.getRequestDispatcher("detail.jsp?curPage="+curPage).forward(request,
						response);
			}
			
		} else {
			request.getRequestDispatcher("error.jsp")
					.forward(request, response);
		}
	}
}
